import { Injectable, TemplateRef } from '@angular/core'
import { RiskRuleModeEnum, LevelOptions } from '@shared/interfaces/risk-control'
import { IFormItem, FormItemType } from '@shared/interfaces/common'
import { FormGroup, FormBuilder, Validators } from '@angular/forms'
import { riskControlRuleQuotaReg, riskControlRuleMax, riskControlRuleMin, riskControlRuleNameLength } from 'app/utils/contraints'
import { I18NService } from '@core'

@Injectable()
export class RiskRuleFormService {
  /** 退款维度 */
  levelOptions = LevelOptions

  tmpObj: { [key: string]: TemplateRef<void> }

  constructor(public fb: FormBuilder, private i18n: I18NService) {}

  initTemplates(tmpObj: { [key: string]: TemplateRef<void> }): void {
    this.tmpObj = tmpObj
  }

  getFormItems(mode: RiskRuleModeEnum): IFormItem[] {
    switch (mode) {
      case RiskRuleModeEnum.Trade:
        return this.getTradeFormItems()
      case RiskRuleModeEnum.Day:
        return this.getDayFormItems()
      case RiskRuleModeEnum.Month:
        return this.getMonthFormItems()
      case RiskRuleModeEnum.Refund:
        return this.getRefundFormItems()
    }
  }

  getValidateForm(mode: RiskRuleModeEnum): FormGroup {
    switch (mode) {
      case RiskRuleModeEnum.Trade:
        return this.getTradeValidateForm()
      case RiskRuleModeEnum.Day:
        return this.getDayValidateForm()
      case RiskRuleModeEnum.Month:
        return this.getMonthValidateForm()
      case RiskRuleModeEnum.Refund:
        return this.getRefundValidateForm()
    }
  }

  private getTradeFormItems(): IFormItem[] {
    return [
      {
        label: this.trans('RISK_CTRL.RULE.NAME'),
        formControlName: 'name',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: this.trans('RISK_CTRL.RULE.NAME') }),
          maxlength: this.trans('VALIDATION.MAX_LENGTH', { length: riskControlRuleNameLength }),
        },
      },
      {
        label: this.trans('GENERAL.INFO.QUOTA.DYNAMIC_CODE'),
        formControlName: 'quota',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: this.trans('GENERAL.INFO.QUOTA.DYNAMIC_CODE') }),
          min: this.trans('VALIDATION.MIN_VALUE', { value: riskControlRuleMin }),
          max: this.trans('VALIDATION.MAX_VALUE', { value: riskControlRuleMax }),
          pattern: this.trans('VALIDATION.PATTERN.INTEGER_ONLY'),
        },
        inputType: 'number',
      },
      {
        label: this.trans('GENERAL.INFO.QUOTA.STATIC_CODE'),
        formControlName: 'quotaStatic',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: this.trans('GENERAL.INFO.QUOTA.STATIC_CODE') }),
          min: this.trans('VALIDATION.MIN_VALUE', { value: riskControlRuleMin }),
          max: this.trans('VALIDATION.MAX_VALUE', { value: riskControlRuleMax }),
          pattern: this.trans('VALIDATION.PATTERN.INTEGER_ONLY'),
        },
        inputType: 'number',
      },
      {
        label: this.trans('GENERAL.INFO.TRANSACTION_CURRENCY'),
        formControlName: 'currency',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED_SELECT', { name: this.trans('GENERAL.INFO.TRANSACTION_CURRENCY') }),
        },
        type: FormItemType.TEMPLATE,
        template: this.tmpObj['currency'],
      },
    ]
  }

  private getDayFormItems(): IFormItem[] {
    return [
      {
        label: this.trans('RISK_CTRL.RULE.NAME'),
        formControlName: 'name',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: this.trans('RISK_CTRL.RULE.NAME') }),
          maxlength: this.trans('VALIDATION.MAX_LENGTH', { length: riskControlRuleNameLength }),
        },
      },
      {
        label: this.trans('GENERAL.INFO.QUOTA'),
        formControlName: 'quota',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: this.trans('GENERAL.INFO.QUOTA') }),
          min: this.trans('VALIDATION.MIN_VALUE', { value: riskControlRuleMin }),
          max: this.trans('VALIDATION.MAX_VALUE', { value: riskControlRuleMax }),
          pattern: this.trans('VALIDATION.PATTERN.INTEGER_ONLY'),
        },
        // inputType: 'number',
      },
      {
        label: this.trans('GENERAL.INFO.TRANSACTION_CURRENCY'),
        formControlName: 'currency',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED_SELECT', { name: this.trans('GENERAL.INFO.TRANSACTION_CURRENCY') }),
        },
        type: FormItemType.TEMPLATE,
        template: this.tmpObj['currency'],
      },
    ]
  }

  private getMonthFormItems(): IFormItem[] {
    return [
      {
        label: this.trans('RISK_CTRL.RULE.NAME'),
        formControlName: 'name',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: this.trans('RISK_CTRL.RULE.NAME') }),
          maxlength: this.trans('VALIDATION.MAX_LENGTH', { length: riskControlRuleNameLength }),
        },
      },
      {
        label: this.trans('GENERAL.INFO.QUOTA'),
        formControlName: 'quota',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: this.trans('GENERAL.INFO.QUOTA') }),
          min: this.trans('VALIDATION.MIN_VALUE', { value: riskControlRuleMin }),
          max: this.trans('VALIDATION.MAX_VALUE', { value: riskControlRuleMax }),
          pattern: this.trans('VALIDATION.PATTERN.INTEGER_ONLY'),
        },
        // inputType: 'number',
      },
      {
        label: this.trans('GENERAL.INFO.TRANSACTION_CURRENCY'),
        formControlName: 'currency',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED_SELECT', { name: this.trans('GENERAL.INFO.TRANSACTION_CURRENCY') }),
        },
        type: FormItemType.TEMPLATE,
        template: this.tmpObj['currency'],
      },
    ]
  }

  private getRefundFormItems(): IFormItem[] {
    return [
      {
        label: this.trans('RISK_CTRL.RULE.NAME'),
        formControlName: 'name',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: this.trans('RISK_CTRL.RULE.NAME') }),
          maxlength: this.trans('VALIDATION.MAX_LENGTH', { length: riskControlRuleNameLength }),
        },
      },
      {
        label: this.trans('RISK_CTRL.LEVEL_CTRL_OF_REFUND'),
        formControlName: 'level',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED_SELECT', { name: this.trans('RISK_CTRL.LEVEL_CTRL_OF_REFUND') }),
        },
        type: FormItemType.SELECT,
        multiple: 'default',
        allowClear: true,
        options: this.levelOptions,
        compareFn: (o1, o2) => o1 === o2,
      },
      {
        label: this.trans('GENERAL.INFO.TRANSACTION_CURRENCY'),
        formControlName: 'currency',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED_SELECT', { name: this.trans('GENERAL.INFO.TRANSACTION_CURRENCY') }),
        },
        type: FormItemType.TEMPLATE,
        template: this.tmpObj['currency'],
      },
    ]
  }

  private getTradeValidateForm(): FormGroup {
    return this.fb.group({
      mode: [RiskRuleModeEnum.Trade, [Validators.required]],
      name: [null, [Validators.required, Validators.maxLength(riskControlRuleNameLength)]],
      quota: [
        null,
        [
          Validators.required,
          Validators.min(riskControlRuleMin),
          Validators.max(riskControlRuleMax),
          Validators.pattern(riskControlRuleQuotaReg),
        ],
      ],
      quotaStatic: [
        null,
        [
          Validators.required,
          Validators.min(riskControlRuleMin),
          Validators.max(riskControlRuleMax),
          Validators.pattern(riskControlRuleQuotaReg),
        ],
      ],
      currency: [null, [Validators.required]],
    })
  }

  private getDayValidateForm(): FormGroup {
    return this.fb.group({
      mode: [RiskRuleModeEnum.Day, [Validators.required]],
      name: [null, [Validators.required, Validators.maxLength(riskControlRuleNameLength)]],
      quota: [
        null,
        [
          Validators.required,
          Validators.min(riskControlRuleMin),
          Validators.max(riskControlRuleMax),
          Validators.pattern(riskControlRuleQuotaReg),
        ],
      ],
      currency: [null, [Validators.required]],
    })
  }

  private getMonthValidateForm(): FormGroup {
    return this.fb.group({
      mode: [RiskRuleModeEnum.Month, [Validators.required]],
      name: [null, [Validators.required, Validators.maxLength(riskControlRuleNameLength)]],
      quota: [
        null,
        [
          Validators.required,
          Validators.min(riskControlRuleMin),
          Validators.max(riskControlRuleMax),
          Validators.pattern(riskControlRuleQuotaReg),
        ],
      ],
      currency: [null, [Validators.required]],
    })
  }

  private getRefundValidateForm(): FormGroup {
    return this.fb.group({
      mode: [RiskRuleModeEnum.Refund, [Validators.required]],
      name: [null, [Validators.required, Validators.maxLength(riskControlRuleNameLength)]],
      level: [null, [Validators.required]],
      currency: [null, [Validators.required]],
    })
  }

  private trans(str: string, params?: Object): string {
    return this.i18n.fanyi(str, params)
  }
}
